<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.List"%>
<%@ page import="com.google.appengine.api.datastore.Key"%>
<%@ page import="com.google.appengine.api.datastore.KeyFactory"%>
<%@ page import="gae.google.tcserver.db.*"%>
<%@ page import="gae.google.tcserver.model.*"%>
<%@ page import="gae.google.tcserver.api.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link type="text/css" href="../CSS/TeamCalendar.css" rel="stylesheet" />
<title>Team Calendar</title>
</head>
<body class="body">
<%
	HttpSession s = request.getSession(false);
	Key userKeyId = (Key)s.getAttribute("userkeyid");
	Boolean admin = false;
	if (userKeyId != null){
		try {
			if (userKeyId.toString().contains("administrator")){
				admin = true;
			}else{
				User user = GetFromDB.getUser(userKeyId);
				admin = user.getUserAdministrator();
			}
		}catch (Exception e) {
	    }
	}

	// holds a list of teams
	List<TeamBean> teams = new ArrayList<TeamBean>();

	String teamKeyId = null;
	long noOfTeams = 0;

	// get json
	CreateJson createJson = new CreateJson();
	String json = createJson.getTeams();
	
	// did we get a json
	if (json != null) {
		// yes, parse json to get all polls
		ParseJson jsonParser = new ParseJson();
		teams =  jsonParser.parseTeams(json);
		if (teams != null) {
			noOfTeams = teams.size();
		}
	}
%>
	<div class="mainContainer">
    	
        <div class="topContainer">
        	<img src="../PICS/TeamCalendar.jpg" style="height: 112px; width: 1100px"\> 
        </div>
        
        <div class="mainMenuContainer">
	        <%if (admin){%>
	        	<a href="/views/Register.jsp">Register</a>
	        	<a href="/views/Options.jsp">Options</a>
	        <%}else{%>
	        	<a href="/views/CreateTeam.jsp">Create Team</a>
	            <a href="/Logout.do">Logout</a>
	        <%}%>
        </div>
		<%String teamId;%>
		<%String url;%>
		<%String image;%>
		<%for (TeamBean team : teams) {%>
			<%teamId = team.getId();%>
			<%url = "/views/TeamMembers.jsp?id=" + teamId;%>
			<%image = team.getImageName();%>
			<%if (image == null || image.contains("Image Name") || image.length() == 0){%>
				<%image = "../PICS/NoImageSelected.jpg";%>
			<%}else{%>
				<%image = "../PICS/" + team.getImageName();%>
			<%}%>
	 		<div id="teams">
				<p><%=team.getName()%></p>
		        <a href=<%=url%>>
				<img src=<%=image%> style="border:1px solid LightGrey; height: 150px; width: 200px"></img></a> 
			</div>
		<%}%>
	</div>
</body>
</html>